﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace UI
{
    /// <summary>
    /// 亚丝娜厨房
    /// </summary>
    public class UIActivityAsunaKitchen : UIActivityTemplate
    {
        public UILabel labLunchTime;
        public UILabel labDinnerTime;
        public GameObject btnGet;
        //public UISprite wordOnBtn;

        System.DateTime startTime1;
        System.DateTime startTime2;
        System.DateTime endTime1;
        System.DateTime endTime2;

        int state1 = 0;
        int state2 = 0;
        bool flag;
        public override void Show()
        {
            base.Show();

            this.activityType = ActivityType.AsunaKitchen;

            CSStaminaRefill msg = new CSStaminaRefill();

            NetAPI.SendHttp(OpDefine.CSStaminaRefill, msg, MsgCallback);
        }

        protected override void PrepareData()
        {

        }

        void MsgCallback(NetWork.Packets.Packet packet, bool success)
        {
            if(success)
            {
                SCStaminaRefill msg = packet.kBody as SCStaminaRefill;
                UpdateLocalData(msg.dataList);

                labLunchTime.text = string.Format("{0:d2}:{1:d2} - {2:d2}:{3:d2}", startTime1.Hour, startTime1.Minute, endTime1.Hour, endTime1.Minute);
                labDinnerTime.text = string.Format("{0:d2}:{1:d2} - {2:d2}:{3:d2}", startTime2.Hour, startTime2.Minute, endTime2.Hour, endTime2.Minute);

                btnGet.SetActive(flag);
            }
        }
        
        void UpdateLocalData(List<ActivityStaminaData> dataList)
        {
            if (dataList != null && dataList.Count >= 2)
            {
                if (dataList[0].startTime < dataList[1].startTime)
                {
                    startTime1 = ServerTimerTool.Java2CSTimeLocale(dataList[0].startTime);
                    endTime1 = ServerTimerTool.Java2CSTimeLocale(dataList[0].endTime);
                    startTime2 = ServerTimerTool.Java2CSTimeLocale(dataList[1].startTime);
                    endTime2 = ServerTimerTool.Java2CSTimeLocale(dataList[1].endTime);
                    state1 = dataList[0].status;
                    state2 = dataList[1].status;
                }
                else
                {
                    startTime1 = ServerTimerTool.Java2CSTimeLocale(dataList[1].startTime);
                    endTime1 = ServerTimerTool.Java2CSTimeLocale(dataList[1].endTime);
                    startTime2 = ServerTimerTool.Java2CSTimeLocale(dataList[0].startTime);
                    endTime2 = ServerTimerTool.Java2CSTimeLocale(dataList[0].endTime);
                    state1 = dataList[1].status;
                    state2 = dataList[0].status;
                }

                //过时了但小红点还在，就取消
                bool value = (ServerTimerTool.CurrentJavaTime() < ServerTimerTool.CSTime2Java(endTime1) && ServerTimerTool.CurrentJavaTime() > ServerTimerTool.CSTime2Java(startTime1) && state1 == 1 || ServerTimerTool.CurrentJavaTime() < ServerTimerTool.CSTime2Java(endTime2) && ServerTimerTool.CurrentJavaTime() > ServerTimerTool.CSTime2Java(startTime2) && state2 == 1);
                if (!value && StaticData.redTips.GetStateByType(RedPointTip.Red.moonlightRestaurant))
                {
                    StaticData.redTips.SetPointState(RedPointTip.Red.moonlightRestaurant, 0);
                    StaticData.redTips.ReduceActivitys();
                }
            }
        }

        public void OnGetBtn()
        {
            CSStaminaGet msg = new CSStaminaGet();
            NetAPI.SendHttp(OpDefine.CSStaminaGet, msg, GetMsgCallback);
        }

        void GetMsgCallback(NetWork.Packets.Packet packet, bool success)
        {
            if(success)
            {
                SCStaminaGet msg = packet.kBody as SCStaminaGet;
                StaticData.playerData.AddResultObj(msg.get);

                bool showget = false;
                int staminaNum = 0;
                foreach(var item in msg.get.simps)
                {
                    if(item.id.CompareTo(Dict.Config.TableConst.stamina) != 0)
                    {
                        showget = true;
                    }
                    else
                    {
                        staminaNum = item.num;
                    }
                }

                if(showget)
                {
                    StartCoroutine(ShowGet(msg.get, staminaNum));
                }
                else
                {
                    BoxManager.CreatePopupTextBox(TableTextBlo.GetText("power") + "+" + staminaNum.ToString());
                }

                UpdateLocalData(msg.dataList);
                StaticData.redTips.SetPointState(RedPointTip.Red.moonlightRestaurant, 0);
                StaticData.redTips.ReduceActivitys();
            }
        }

        IEnumerator ShowGet(ResultObject objs,int staminaNum)
        {
            CommonMethod.ShowGetObjs(objs, TableTextBlo.GetText("lucky_award"));

            yield return new WaitForSeconds(0.7f);

            BoxManager.CreatePopupTextBox(TableTextBlo.GetText("power") + "+" + staminaNum.ToString());
        }
        
        void Update()
        {
            bool value = (ServerTimerTool.CurrentJavaTime() < ServerTimerTool.CSTime2Java(endTime1) && ServerTimerTool.CurrentJavaTime() > ServerTimerTool.CSTime2Java(startTime1) && state1 == 1 ||ServerTimerTool.CurrentJavaTime() < ServerTimerTool.CSTime2Java(endTime2) && ServerTimerTool.CurrentJavaTime() > ServerTimerTool.CSTime2Java(startTime2) && state2 == 1);
            if (flag != value)
            {
                if (flag && StaticData.redTips.GetStateByType(RedPointTip.Red.moonlightRestaurant))
                {
                    StaticData.redTips.SetPointState(RedPointTip.Red.moonlightRestaurant, 0);
                    StaticData.redTips.ReduceActivitys();
                }
                flag = value;
                btnGet.SetActive(value);
            }
        }
    }
}
